package com.stox.charting.ui.swing.model;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import com.stox.charting.ui.swing.view.ChartView;
import com.stox.core.util.Constant;

public class DefaultAxis extends Axis {
	private static final long serialVersionUID = 8853671456155975292L;
	private static final int markWidth = 5;
	private static final int textHorizontalGap = 3;
	private static final int textVerticalGap = 3;
	
	
	protected double max;
	protected double min;
	
	public DefaultAxis(ChartView chartView, 
			Orientation orientation,
			String borderLayoutConstraint) {
		super(chartView, orientation, borderLayoutConstraint);
	}
	
	public DefaultAxis(ChartView chartView, 
			Orientation orientation,
			String borderLayoutConstraint,
			Grid grid) {
		super(chartView, orientation, borderLayoutConstraint);
	}

	public void update(double maximumValueVisible, 
			double minimumValueVisible){
		max = maximumValueVisible;
		min = minimumValueVisible;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		int count = 10;
		int minHeight = 20;
		Rectangle r = getVisibleRect();

		while(r.getHeight() < minHeight*count) count--;
		
		double increment = (max - min)/count;
		double mult = Math.pow(10,Math.floor(Math.log10(increment)));
		double v[] = new double[]{1,2,2.5,5};
		
		for(int d = 1; d < v.length; d++){
			if(increment >= v[d-1]*mult && increment < v[d]*mult){
				increment = v[d-1]*mult;
				break;
			}
		}
		
		String letter = null;
		double devisor = 1;
		if(max > 1000000000){
			letter = "B";
			devisor = 1000000000;
		}else if(max > 1000000){
			letter = "M";
			devisor = 1000000;
		}else if(max > 1000){
			letter = "K";
			devisor = 1000;
		}
		
		
		int lastTextY = Integer.MAX_VALUE;
		g.setFont(Theme.getFonts().getSmallFont());
		int height = (int)g.getFont().getStringBounds("M", ((Graphics2D)g).getFontRenderContext()).getHeight();
		
		
		for(double value = min; value <= max; value+=increment){
			int y = (int)(r.getHeight() - ((value - min)*r.getHeight())/(max-min) - r.getY());
			if(y < lastTextY - height - textVerticalGap && y >= r.y){
				
				if(Orientation.VERTICAL.equals(getOrientation()) 
						&& BorderLayout.WEST.equals(getBorderLayoutConstraints())){
					g.setColor(Theme.getColours().getAxisLine());
					g.drawLine((int)(r.getX()+r.getWidth()), y, (int)(r.getX()- markWidth + r.getWidth()), y);
					String text =Constant.numberFormat.format(value/devisor)+(letter == null?"":letter);
					int width = (int)g.getFont().getStringBounds(text, ((Graphics2D)g).getFontRenderContext()).getWidth();
					g.setColor(Theme.getColours().getAxisText());
					g.drawString(text,(int)(r.getX()+r.getWidth()-width-textHorizontalGap),y+height+textVerticalGap);
				}else if(Orientation.VERTICAL.equals(getOrientation()) 
						&& BorderLayout.EAST.equals(getBorderLayoutConstraints())){
					g.setColor(Theme.getColours().getAxisLine());
					g.drawLine((int)r.getX(), y, (int)r.getX()+ markWidth, y);
					g.setColor(Theme.getColours().getAxisText());
					g.drawString(Constant.numberFormat.format(value/devisor)+(letter == null?"":letter),(int)(r.getX() + textHorizontalGap),y+height+textVerticalGap);
				}
				
				lastTextY = y;
			}
		}
		
		
		
	}
	
}
